package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.itboys.mobile.dto.count.SundayCountTotalDto;
import org.itboys.mobile.service.mongo.front.SundayFrontCartService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：jiangxiong
 * 日期：2017年2月9日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_购物车_controller。
 *
 */
@Controller
@RequestMapping("/sunday/weixin/cart")
public class SundayWeiXinCartController extends BaseController {
    @Autowired
    private SundayFrontCartService cartService;


    /**
     * 获取购物车。购物车ID集合参数说明：可传入此参数，核算购物车总价（如系统含满减，多件折扣等促销）
     * @param request
     * @param cartIds

     */
    @RequestMapping(value="/index",method={ RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【微信端】获取购物车。购物车ID集合参数说明：可传入此参数，核算购物车总价（如果系统含满减，多件折扣等促销）。")
    public String String(HttpServletRequest request,Model model,
             @ApiParam(required = false, name = "cartIds", value = "购物车ID集合(非商品ID)。如：cardIds=1,2,3。可传入此参数，核算购物车总价（如系统含满减，多件折扣等促销）") @RequestParam(value = "cartIds", required = false) List<Long> cartIds
    ) {
        try{
            SundayMemberSessionDto memberSessionDto = AccountIdHoldUtils.getSessionMember(request);
            SundayCountTotalDto result = cartService.getCart(memberSessionDto.getId(), cartIds, SundayCommonConstants.cart_type_cart);
            model.addAttribute("result",result);
            return "/aigou/weixin/cart/index";
        }catch (Exception e){
            logger.error("/sunday/weixin/cart/index----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }

    /**
     * 【微信端】加入购物车（支持批量新增）。商品ID集合和数量集合，需要对应。
     * @param request
     * @param response
     * @param productIds
     * @param nums

     */
    @RequestMapping(value="/addCart",method={ RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【微信端】加入购物车（支持批量新增）。商品ID集合和数量集合，需要对应。")
    public void addCart(HttpServletRequest request,
                            HttpServletResponse response,
                       // @ApiParam(required = true, name = "memberId", value = "用户ID") @RequestParam(value="memberId",required=true) Long memberId,
                       // @ApiParam(required = false, name = "villageId", value = "小区ID。用户版传入") @RequestParam(value="villageId",required=false) Long villageId,
                        @ApiParam(required = true, name = "productIds", value = "商品ID集合，和数量集合对应。【参与签名】。如：productIds=1,2,3") @RequestParam(value = "productIds", required = true) List<Long> productIds,
                        @ApiParam(required = true, name = "nums", value = "数量集合，和商品ID对应。【参与签名】。如：nums=1,2,3") @RequestParam(value = "nums", required = true) List<Integer> nums,
                        @ApiParam(required = true, name = "cartType", value = "加入购车类型。1,加入购物车，2立即购买加入购物车") @RequestParam(value="cartType",required=true) Integer cartType) {
        try{
           SundayMemberSessionDto memberSessionDto = AccountIdHoldUtils.getSessionMember(request);
            List<Long> carIds = cartService.addCart(memberSessionDto.getId(), productIds , nums, cartType);
            ResponseMessage.success(carIds);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/cart/addCart----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 【微信端】修改购物车数量（支持批量修改）。商品ID集合和数量集合，需要对应
     * @param request
     * @param response
     * @param cartIds
     * @param nums
     */
    @RequestMapping(value="/updateCart",method={ RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【微信端】修改购物车数量（支持批量修改）。商品ID集合和数量集合，需要对应。")
    public void updateCart(HttpServletRequest request,
                        HttpServletResponse response,
                 //       @ApiParam(required = false, name = "villageId", value = "小区ID.用户版传入") @RequestParam(value="villageId",required=false) Long villageId,
                        @ApiParam(required = true, name = "cartIds", value = "购物车ID集合(非商品ID)。【参与签名】。如：cardIds=1,2,3") @RequestParam(value = "cartIds", required = true) List<Long> cartIds,
                        @ApiParam(required = true, name = "nums", value = "数量集合（修改后的数量）。【参与签名】，和购物车ID对应。如：numbers=1,2,3") @RequestParam(value = "nums", required = true) List<Integer> nums) {
        try{
            SundayMemberSessionDto memberSessionDto = AccountIdHoldUtils.getSessionMember(request);
            cartService.updateCart( memberSessionDto.getId(), cartIds ,nums);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/cart/updateCart----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 【微信端】修改购物车数量（支持批量修改）。商品ID集合和数量集合，需要对应
     * @param request
     * @param response
     * @param cartIds
     */
    @RequestMapping(value="/deleteCart",method={ RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【微信端】删除购物车（支持批量删除）。")
    public void deleteCart(HttpServletRequest request,
                           HttpServletResponse response,
                           @ApiParam(required = true, name = "cartIds", value = "购物车ID集合(非商品ID)。【参与签名】。如：cardIds=1,2,3") @RequestParam(value = "cartIds", required = true) List<Long> cartIds) {

        try{
            SundayMemberSessionDto memberSessionDto = AccountIdHoldUtils.getSessionMember(request);
            cartService.deleteCart(cartIds , memberSessionDto.getId(), SundayCommonConstants.cart_type_cart);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/cart/deleteCart----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
}
